package com.lry.basic.insterest;

import net.coobird.thumbnailator.Thumbnails;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author pibigstar
 * @desc 海报生成工具
 **/
public class PosterUtil {
    private static final String result_img = "D:/demo.png"; // 最终输出图片
//    private static final String QQ = "953506233";
//    private static final String avatar_img = "http://q1.qlogo.cn/g?b=qq&nk="+QQ+"&s=100";//QQ获取头像接口
//https://images.xiaogemaicai.cn/20180531/11341589679d15.jpg 玉米
//https://images.xiaogemaicai.cn/20180531/10314139496fab.jpg 鸡腿

    public static void main(String[] args) throws Exception {
        String qrCodeUrl = "https://images.xiaogemaicai.cn/StoreQRCodes/201001.jpg";
        String goodsUrl = "https://images.xiaogemaicai.cn/20180531/11341589679d15.jpg";
        String avatarUrl = "https://wx.qlogo.cn/mmopen/vi_32/bvlq2OnCCSQxpfyJkgvSn3AvHaqB5UFFTasHWZD4ibeHb7b5wW6bVkqbdtTiaEw45GfLYibudiaORHBrRsy9tKgabA/132";
        int width = 1440;
        int height = 2560;
        String name = "会飞的鱼";
        String desc = "新鲜玉米 250g";
        String price = "￥6.99";
        Poster poster = initPoster(width, height,avatarUrl,qrCodeUrl,goodsUrl,name,desc,price);
        drawPoster(poster);
    }

    private static Poster initPoster(int width, int height, String avatarUrl, String qrCodeUrl, String goodsUrl, String name, String desc, String price) {
        Poster poster = new Poster();
        //画布
        poster.setWidth(width);
        poster.setHeight(height);
        //头像
        poster.setAvatarUrl(avatarUrl);
        poster.setCircle(true);
        poster.setAvatarX((int)(width/11.5));
        poster.setAvatarY(height/28);
        poster.setAvatarWidth(width/6);
        poster.setAvatarHeight(width/6);
        //名字
        poster.setName(name);
        poster.setNameFont(new Font("宋体",Font.PLAIN,width/24));
        poster.setNameColor(new Color(33,33,33));
        poster.setNameX(poster.getAvatarX()+poster.getAvatarWidth()+20);
        poster.setNameY(poster.getAvatarY()+poster.getAvatarHeight()/2+15);
        //商品
        poster.setGoodsUrl(goodsUrl);
        poster.setGoodsWidth((int)(width/1.35));
        poster.setGoodsHeight((int)(height/3.4));
        poster.setGoodsX((width-poster.getGoodsWidth())/2);
        poster.setGoodsY(poster.getAvatarY()+poster.getAvatarHeight()+100);
        //商品详情
        poster.setDesc(desc);
        poster.setDescColor(Color.BLACK);
        int size = width/17;
        poster.setDescFont(new Font("宋体",Font.BOLD,size));
        poster.setDescX((width-desc.length()*size)/2);
        poster.setDescY(poster.getGoodsY()+poster.getGoodsHeight()+(int)(height/21.3));
        //价格
        poster.setPrice(price);
        poster.setPriceColor(Color.RED);
        poster.setDescFont(new Font("宋体",Font.BOLD,size));
        poster.setPriceX((width-price.length()*size)/2);
        poster.setPriceY(poster.getGoodsY()+poster.getGoodsHeight()+(int)(height/10.6));
        //小程序码
        poster.setQrCodeUrl(qrCodeUrl);
        poster.setQrCodeWidth((int)(width/2.85));
        poster.setQrCodeHeight((int)(width/2.85));
        poster.setQrCodeX((int)(width/11.5));
        poster.setQrCodeY(height-poster.getQrCodeHeight()-(int)(height/7.68));
        //tips1
        poster.setTip1("长按识别小程序码");
        poster.setTip1Color(Color.BLACK);
        poster.setTip1Font(new Font("宋体",Font.BOLD,width/21));
        poster.setTip1X(poster.getQrCodeX()+poster.getQrCodeWidth()+20);
        poster.setTip1Y(poster.getQrCodeY()+poster.getQrCodeHeight()/2+10);
        //tips2
        poster.setTip2("好物与好友一起分享");
        poster.setTip2Color(Color.GRAY);
        poster.setTip2Font(new Font("宋体",Font.PLAIN,width/27));
        poster.setTip2X(poster.getQrCodeX()+poster.getQrCodeWidth()+25);
        poster.setTip2Y(poster.getQrCodeY()+poster.getQrCodeHeight()/2+60);
        //footer
        poster.setFooterColor(new Color(241,112,25));
        poster.setFooterWidth(width);
        poster.setFooterHeight(height/13);
        poster.setFooterX(0);
        poster.setFooterY(height-poster.getFooterHeight());
        //footer tips
        poster.setFooterTip("帮您处理买菜这点事");
        poster.setFooterTipColor(Color.WHITE);
        poster.setFooterTipFont(new Font("宋体",Font.BOLD,width/21));
        poster.setFooterTipX((width-(poster.getFooterTip().length()*width/21))/2);
        poster.setFooterTipY(height-poster.getFooterHeight()/3);
        return poster;
    }

    private static void drawPoster(Poster poster) throws Exception {
        long startTime = System.currentTimeMillis();
        String qrCodeUrl = poster.getQrCodeUrl();
        String goodsUrl = poster.getGoodsUrl();
        String avatarUrl = poster.getAvatarUrl();
        BufferedImage qrCodeImage = ImageIO.read(new URL(qrCodeUrl));
        BufferedImage goodsImage = ImageIO.read(new URL(goodsUrl));
        int width = poster.getWidth();
        int height = poster.getHeight();
        //画布
        BufferedImage canvas = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        Graphics2D g = (Graphics2D) canvas.getGraphics();
        g.setBackground(Color.WHITE);//设置背景色
        g.clearRect(0, 0, width, height);

        // 设置文字抗锯齿
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //圆形头像
        BufferedImage newAvatar = circle(avatarUrl,poster.getAvatarWidth());
        //画头像
        g.drawImage(newAvatar.getScaledInstance(newAvatar.getWidth(), newAvatar.getHeight(), Image.SCALE_SMOOTH), poster.getAvatarX(), poster.getAvatarY(), null);
        // 4. 写字（昵称）
        g.setColor(poster.getNameColor());
        g.setFont(poster.getNameFont());
        g.drawString(poster.getName(), poster.getNameX(), poster.getNameY());
        //画商品
//        g.drawImage(Thumbnails.of(goodsImage).size(poster.getGoodsWidth(), poster.getGoodsHeight()).asBufferedImage(), poster.getGoodsX(), poster.getGoodsY(), null);
        g.drawImage(goodsImage.getScaledInstance(poster.getGoodsWidth(), poster.getGoodsHeight(), Image.SCALE_SMOOTH), poster.getGoodsX(), poster.getGoodsY(), null);
        //画商品详情
        g.setColor(poster.getDescColor());
        g.setFont(poster.getDescFont());
        g.drawString(poster.getDesc(), poster.getDescX(), poster.getDescY());
        //画价格
        g.setColor(poster.getPriceColor());
        g.setFont(poster.getPriceFont());
        g.drawString(poster.getPrice(), poster.getPriceX(), poster.getPriceY());
        //画小程序码
        g.drawImage(qrCodeImage.getScaledInstance(poster.getQrCodeWidth(), poster.getQrCodeHeight(), Image.SCALE_SMOOTH),
                poster.getQrCodeX(), poster.getQrCodeY(), null);
        //画tips1
        g.setColor(poster.getTip1Color());
        g.setFont(poster.getTip1Font());
        g.drawString(poster.getTip1(), poster.getTip1X(), poster.getTip1Y());
        //画tips2
        g.setColor(poster.getTip2Color());
        g.setFont(poster.getTip2Font());
        g.drawString(poster.getTip2(), poster.getTip2X(), poster.getTip2Y());
        //画底部栏
        g.setColor(poster.getFooterColor());
        g.fillRect(poster.getFooterX(),poster.getFooterY(),poster.getFooterWidth(),poster.getFooterHeight());
        //画底部栏提示
        g.setColor(poster.getFooterTipColor());
        g.setFont(poster.getFooterTipFont());
        g.drawString(poster.getFooterTip(), poster.getFooterTipX(), poster.getFooterTipY());
        g.dispose();
        File resultImg = new File("D:\\demo.png");
        ImageIO.write(canvas, "png", resultImg);

        System.out.println("生成成功！");
        System.out.println("耗时: " + (System.currentTimeMillis()-startTime)/1000.0 + "s");
        System.out.println("生成文件路径: " + resultImg.getAbsolutePath());
    }


    private static BufferedImage circle(String avatar_img,int width) throws Exception {
        BufferedImage avatar = ImageIO.read(new URL(avatar_img));
        BufferedImage newAvatar = new BufferedImage(width, width, BufferedImage.TYPE_INT_ARGB);
        Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, width, width);
        Graphics2D g2 = newAvatar.createGraphics();
        newAvatar = g2.getDeviceConfiguration().createCompatibleImage(width, width, Transparency.TRANSLUCENT);
        g2 = newAvatar.createGraphics();
        g2.setComposite(AlphaComposite.Clear);
        g2.fill(new Rectangle(newAvatar.getWidth(), newAvatar.getHeight()));
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 1.0f));
        g2.setClip(shape);
        // 使用 setRenderingHint 设置抗锯齿
        g2 = newAvatar.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.fillRoundRect(0, 0,width, width, width, width);
        g2.setComposite(AlphaComposite.SrcIn);
        g2.drawImage(avatar, 0, 0, width, width, null);
        g2.dispose();
        return newAvatar;
    }
}
